﻿using ShooterGameMOD_Besiege.Player;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace ShooterGameMOD_Besiege.Weapon
{
    class CrossBowScript : MonoBehaviour
    {
        //使用位置和方向
        public static Vector3 usingPosition = new Vector3(1.5f, 1f, 3f);
        public static Vector3 usingRotation = Vector3.right * -90f;
        //未使用位置和方向
        public static Vector3 nonusePosition = new Vector3(0f, 1f, -1f);
        public static Vector3 nonuseRotation = Vector3.right * 0f;

        public int bulletNumber = 30,
            bulletMaxNumber = 30,
            currentBulletNumber;
        public float fireRate = 24f;
        public float power = 12f;
        public WeaponScript.WeaponKind weaponKind = WeaponScript.WeaponKind.FirstWeapon;

        Rigidbody rigibody;
        CrossBowBlock crossBowBlock;
        GameObject player;
        PlayerControllerScript playerControllerScript;
        PlayerWeaponScript playerWeapon;

        private void Start()
        {
            rigibody = GetComponent<Rigidbody>();
            crossBowBlock = GetComponent<CrossBowBlock>();
            player = PlayerScript.player;
            playerWeapon = player.GetComponent<PlayerWeaponScript>();


            rigibody.isKinematic = true;
            transform.SetParent(player.transform);
            if (playerWeapon.currentWeaponKind == WeaponScript.WeaponKind.FirstWeapon)
            {
                setUseingPosition();
            }
            else
            {
                setUonusePosition();
            }
            

            playerControllerScript = player.GetComponent<PlayerControllerScript>();

            //crossBowBlock.KeyList[0].AddOrReplaceKey(0, playerControllerScript.Fire);
            //crossBowBlock.Keys.First().AddOrReplaceKey(0, playerControllerScript.Fire);
            //crossBowBlock.Sliders. Find(match => match.Key == "rate-of-fire").Value = fireRate;
            foreach (var s in crossBowBlock.Sliders)
            {
                if (s.Key == "rate-of-fire")
                {
                    s.Value = fireRate;
                }
                if(s.Key == "power")
                {
                    s.Value = power;
                }
            }
            //crossBowBlock.Sliders.Find(match => match.Key == "power").Value = power;
            currentBulletNumber = bulletNumber = Mathf.Min(crossBowBlock.ammo,bulletMaxNumber);
            //foreach (var ms in crossBowBlock.Sliders)
            //{
            //    Debug.Log(ms.Key);
            //}
        }

        private void Update()
        {

            if (StatMaster.levelSimulating && playerWeapon.currentWeaponKind == WeaponScript.WeaponKind.FirstWeapon)
            {
                if (Input.GetKeyDown(playerControllerScript.dropWeapon) && playerWeapon.firstWeapon != null)
                {
                    Debug.Log("丢武器");

                    rigibody.isKinematic = false;
                    rigibody.AddRelativeForce(new Vector3(0, -750f, 500f));
                    crossBowBlock.Keys.First().AddOrReplaceKey(0, KeyCode.None);
                    transform.SetParent(Machine.Active().SimulationMachine);
                    playerWeapon.firstWeapon = null;
                    playerWeapon.currentWeapon = null;
                    playerWeapon.currentWeaponKind = WeaponScript.WeaponKind.None;
                    Destroy(gameObject.GetComponent("CrossBowScript"));

                }

                if (playerWeapon.currentWeaponKind == WeaponScript.WeaponKind.FirstWeapon)
                {
                    crossBowBlock.enabled = true;
                }
                else
                {
                    crossBowBlock.enabled = false;
                }

                currentBulletNumber = crossBowBlock.ammo;


            }
           



        }

        public void setUseingPosition()
        {
            transform.position = player.transform.TransformPoint(usingPosition);
            transform.rotation = player.transform.rotation;
            transform.Rotate(usingRotation, Space.Self);
        }

        public void setUonusePosition()
        {
            transform.position = player.transform.TransformPoint(nonusePosition);
            transform.rotation = player.transform.rotation;
            transform.Rotate(nonuseRotation, Space.Self);
        }
    }
}
